3 * Default wiring for MediaWiki services.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
22 * This file is loaded by MediaWiki\MediaWikiServices::getInstance() during the
23 * bootstrapping of the dependency injection framework.
25 * This file returns an array that associates service name with instantiator functions
26 * that create the default instances for the services used by MediaWiki core.
27 * For every service that MediaWiki core requires, an instantiator must be defined in
30 * @note As of version 1.27, MediaWiki is only beginning to use dependency injection.
31 * The services defined here do not yet fully represent all services used by core,
32 * much of the code still relies on global state for this accessing services.
36 * @see docs/injection.txt for an overview of using dependency injection in the
37 * MediaWiki code base.
40 use MediaWiki\Interwiki\ClassicInterwikiLookup
;
41 use MediaWiki\Linker\LinkRendererFactory
;
42 use MediaWiki\Logger\LoggerFactory
;
43 use MediaWiki\MediaWikiServices
;
44 use MediaWiki\Shell\CommandFactory
;
45 use MediaWiki\Storage\BlobStoreFactory
;
46 use MediaWiki\Storage\RevisionStore
;
47 use MediaWiki\Storage\SqlBlobStore
;
50 'DBLoadBalancerFactory' => function ( MediaWikiServices
$services ) {
51 $mainConfig = $services->getMainConfig();
53 $lbConf = MWLBFactory
::applyDefaultConfig(
54 $mainConfig->get( 'LBFactoryConf' ),
56 $services->getConfiguredReadOnlyMode()
58 $class = MWLBFactory
::getLBFactoryClass( $lbConf );
60 return new $class( $lbConf );
63 'DBLoadBalancer' => function ( MediaWikiServices
$services ) {
64 // just return the default LB from the DBLoadBalancerFactory service
65 return $services->getDBLoadBalancerFactory()->getMainLB();
68 'SiteStore' => function ( MediaWikiServices
$services ) {
69 $rawSiteStore = new DBSiteStore( $services->getDBLoadBalancer() );
71 // TODO: replace wfGetCache with a CacheFactory service.
72 // TODO: replace wfIsHHVM with a capabilities service.
73 $cache = wfGetCache( wfIsHHVM() ? CACHE_ACCEL
: CACHE_ANYTHING
);
75 return new CachingSiteStore( $rawSiteStore, $cache );
78 'SiteLookup' => function ( MediaWikiServices
$services ) {
79 $cacheFile = $services->getMainConfig()->get( 'SitesCacheFile' );
81 if ( $cacheFile !== false ) {
82 return new FileBasedSiteLookup( $cacheFile );
84 // Use the default SiteStore as the SiteLookup implementation for now
85 return $services->getSiteStore();
89 'ConfigFactory' => function ( MediaWikiServices
$services ) {
90 // Use the bootstrap config to initialize the ConfigFactory.
91 $registry = $services->getBootstrapConfig()->get( 'ConfigRegistry' );
92 $factory = new ConfigFactory();
94 foreach ( $registry as $name => $callback ) {
95 $factory->register( $name, $callback );
100 'MainConfig' => function ( MediaWikiServices
$services ) {
101 // Use the 'main' config from the ConfigFactory service.
102 return $services->getConfigFactory()->makeConfig( 'main' );
105 'InterwikiLookup' => function ( MediaWikiServices
$services ) {
106 global $wgContLang; // TODO: manage $wgContLang as a service
107 $config = $services->getMainConfig();
108 return new ClassicInterwikiLookup(
110 $services->getMainWANObjectCache(),
111 $config->get( 'InterwikiExpiry' ),
112 $config->get( 'InterwikiCache' ),
113 $config->get( 'InterwikiScopes' ),
114 $config->get( 'InterwikiFallbackSite' )
118 'StatsdDataFactory' => function ( MediaWikiServices
$services ) {
119 return new BufferingStatsdDataFactory(
120 rtrim( $services->getMainConfig()->get( 'StatsdMetricPrefix' ), '.' )
124 'EventRelayerGroup' => function ( MediaWikiServices
$services ) {
125 return new EventRelayerGroup( $services->getMainConfig()->get( 'EventRelayerConfig' ) );
128 'SearchEngineFactory' => function ( MediaWikiServices
$services ) {
129 return new SearchEngineFactory( $services->getSearchEngineConfig() );
132 'SearchEngineConfig' => function ( MediaWikiServices
$services ) {
134 return new SearchEngineConfig( $services->getMainConfig(), $wgContLang );
137 'SkinFactory' => function ( MediaWikiServices
$services ) {
138 $factory = new SkinFactory();
140 $names = $services->getMainConfig()->get( 'ValidSkinNames' );
142 foreach ( $names as $name => $skin ) {
143 $factory->register( $name, $skin, function () use ( $name, $skin ) {
144 $class = "Skin$skin";
145 return new $class( $name );
148 // Register a hidden "fallback" skin
149 $factory->register( 'fallback', 'Fallback', function () {
150 return new SkinFallback
;
152 // Register a hidden skin for api output
153 $factory->register( 'apioutput', 'ApiOutput', function () {
160 'WatchedItemStore' => function ( MediaWikiServices
$services ) {
161 $store = new WatchedItemStore(
162 $services->getDBLoadBalancer(),
163 new HashBagOStuff( [ 'maxKeys' => 100 ] ),
164 $services->getReadOnlyMode()
166 $store->setStatsdDataFactory( $services->getStatsdDataFactory() );
170 'WatchedItemQueryService' => function ( MediaWikiServices
$services ) {
171 return new WatchedItemQueryService( $services->getDBLoadBalancer() );
174 'CryptRand' => function ( MediaWikiServices
$services ) {
175 $secretKey = $services->getMainConfig()->get( 'SecretKey' );
176 return new CryptRand(
178 // To try vary the system information of the state a bit more
179 // by including the system's hostname into the state
181 // It's mostly worthless but throw the wiki's id into the data
182 // for a little more variance
184 // If we have a secret key set then throw it into the state as well
185 function () use ( $secretKey ) {
186 return $secretKey ?
: '';
189 // The config file is likely the most often edited file we know should
190 // be around so include its stat info into the state.
191 // The constant with its location will almost always be defined, as
192 // WebStart.php defines MW_CONFIG_FILE to $IP/LocalSettings.php unless
193 // being configured with MW_CONFIG_CALLBACK (e.g. the installer).
194 defined( 'MW_CONFIG_FILE' ) ?
[ MW_CONFIG_FILE
] : [],
195 LoggerFactory
::getInstance( 'CryptRand' )
199 'CryptHKDF' => function ( MediaWikiServices
$services ) {
200 $config = $services->getMainConfig();
202 $secret = $config->get( 'HKDFSecret' ) ?
: $config->get( 'SecretKey' );
204 throw new RuntimeException( "Cannot use MWCryptHKDF without a secret." );
207 // In HKDF, the context can be known to the attacker, but this will
208 // keep simultaneous runs from producing the same output.
209 $context = [ microtime(), getmypid(), gethostname() ];
211 // Setup salt cache. Use APC, or fallback to the main cache if it isn't setup
212 $cache = $services->getLocalServerObjectCache();
213 if ( $cache instanceof EmptyBagOStuff
) {
214 $cache = ObjectCache
::getLocalClusterInstance();
217 return new CryptHKDF( $secret, $config->get( 'HKDFAlgorithm' ),
218 $cache, $context, $services->getCryptRand()
222 'MediaHandlerFactory' => function ( MediaWikiServices
$services ) {
223 return new MediaHandlerFactory(
224 $services->getMainConfig()->get( 'MediaHandlers' )
228 'MimeAnalyzer' => function ( MediaWikiServices
$services ) {
229 $logger = LoggerFactory
::getInstance( 'Mime' );
230 $mainConfig = $services->getMainConfig();
232 'typeFile' => $mainConfig->get( 'MimeTypeFile' ),
233 'infoFile' => $mainConfig->get( 'MimeInfoFile' ),
234 'xmlTypes' => $mainConfig->get( 'XMLMimeTypes' ),
236 function ( $mimeAnalyzer, &$head, &$tail, $file, &$mime ) use ( $logger ) {
238 $deja = new DjVuImage( $file );
239 if ( $deja->isValid() ) {
240 $logger->info( __METHOD__
. ": detected $file as image/vnd.djvu\n" );
241 $mime = 'image/vnd.djvu';
245 // Some strings by reference for performance - assuming well-behaved hooks
247 'MimeMagicGuessFromContent',
248 [ $mimeAnalyzer, &$head, &$tail, $file, &$mime ]
251 'extCallback' => function ( $mimeAnalyzer, $ext, &$mime ) {
252 // Media handling extensions can improve the MIME detected
253 Hooks
::run( 'MimeMagicImproveFromExtension', [ $mimeAnalyzer, $ext, &$mime ] );
255 'initCallback' => function ( $mimeAnalyzer ) {
256 // Allow media handling extensions adding MIME-types and MIME-info
257 Hooks
::run( 'MimeMagicInit', [ $mimeAnalyzer ] );
262 if ( $params['infoFile'] === 'includes/mime.info' ) {
263 $params['infoFile'] = __DIR__
. "/libs/mime/mime.info";
266 if ( $params['typeFile'] === 'includes/mime.types' ) {
267 $params['typeFile'] = __DIR__
. "/libs/mime/mime.types";
270 $detectorCmd = $mainConfig->get( 'MimeDetectorCommand' );
271 if ( $detectorCmd ) {
272 $params['detectCallback'] = function ( $file ) use ( $detectorCmd ) {
273 return wfShellExec( "$detectorCmd " . wfEscapeShellArg( $file ) );
277 // XXX: MimeMagic::singleton currently requires this service to return an instance of MimeMagic
278 return new MimeMagic( $params );
281 'ProxyLookup' => function ( MediaWikiServices
$services ) {
282 $mainConfig = $services->getMainConfig();
283 return new ProxyLookup(
284 $mainConfig->get( 'SquidServers' ),
285 $mainConfig->get( 'SquidServersNoPurge' )
289 'Parser' => function ( MediaWikiServices
$services ) {
290 $conf = $services->getMainConfig()->get( 'ParserConf' );
291 return ObjectFactory
::constructClassInstance( $conf['class'], [ $conf ] );
294 'ParserCache' => function ( MediaWikiServices
$services ) {
295 $config = $services->getMainConfig();
296 $cache = ObjectCache
::getInstance( $config->get( 'ParserCacheType' ) );
297 wfDebugLog( 'caches', 'parser: ' . get_class( $cache ) );
299 return new ParserCache(
301 $config->get( 'CacheEpoch' )
305 'LinkCache' => function ( MediaWikiServices
$services ) {
306 return new LinkCache(
307 $services->getTitleFormatter(),
308 $services->getMainWANObjectCache()
312 'LinkRendererFactory' => function ( MediaWikiServices
$services ) {
313 return new LinkRendererFactory(
314 $services->getTitleFormatter(),
315 $services->getLinkCache()
319 'LinkRenderer' => function ( MediaWikiServices
$services ) {
322 if ( defined( 'MW_NO_SESSION' ) ) {
323 return $services->getLinkRendererFactory()->create();
325 return $services->getLinkRendererFactory()->createForUser( $wgUser );
329 'GenderCache' => function ( MediaWikiServices
$services ) {
330 return new GenderCache();
333 '_MediaWikiTitleCodec' => function ( MediaWikiServices
$services ) {
336 return new MediaWikiTitleCodec(
338 $services->getGenderCache(),
339 $services->getMainConfig()->get( 'LocalInterwikis' )
343 'TitleFormatter' => function ( MediaWikiServices
$services ) {
344 return $services->getService( '_MediaWikiTitleCodec' );
347 'TitleParser' => function ( MediaWikiServices
$services ) {
348 return $services->getService( '_MediaWikiTitleCodec' );
351 'MainObjectStash' => function ( MediaWikiServices
$services ) {
352 $mainConfig = $services->getMainConfig();
354 $id = $mainConfig->get( 'MainStash' );
355 if ( !isset( $mainConfig->get( 'ObjectCaches' )[$id] ) ) {
356 throw new UnexpectedValueException(
357 "Cache type \"$id\" is not present in \$wgObjectCaches." );
360 return \ObjectCache
::newFromParams( $mainConfig->get( 'ObjectCaches' )[$id] );
363 'MainWANObjectCache' => function ( MediaWikiServices
$services ) {
364 $mainConfig = $services->getMainConfig();
366 $id = $mainConfig->get( 'MainWANCache' );
367 if ( !isset( $mainConfig->get( 'WANObjectCaches' )[$id] ) ) {
368 throw new UnexpectedValueException(
369 "WAN cache type \"$id\" is not present in \$wgWANObjectCaches." );
372 $params = $mainConfig->get( 'WANObjectCaches' )[$id];
373 $objectCacheId = $params['cacheId'];
374 if ( !isset( $mainConfig->get( 'ObjectCaches' )[$objectCacheId] ) ) {
375 throw new UnexpectedValueException(
376 "Cache type \"$objectCacheId\" is not present in \$wgObjectCaches." );
378 $params['store'] = $mainConfig->get( 'ObjectCaches' )[$objectCacheId];
380 return \ObjectCache
::newWANCacheFromParams( $params );
383 'LocalServerObjectCache' => function ( MediaWikiServices
$services ) {
384 $mainConfig = $services->getMainConfig();
386 if ( function_exists( 'apc_fetch' ) ) {
388 } elseif ( function_exists( 'apcu_fetch' ) ) {
390 } elseif ( function_exists( 'xcache_get' ) && wfIniGetBool( 'xcache.var_size' ) ) {
392 } elseif ( function_exists( 'wincache_ucache_get' ) ) {
398 if ( !isset( $mainConfig->get( 'ObjectCaches' )[$id] ) ) {
399 throw new UnexpectedValueException(
400 "Cache type \"$id\" is not present in \$wgObjectCaches." );
403 return \ObjectCache
::newFromParams( $mainConfig->get( 'ObjectCaches' )[$id] );
406 'VirtualRESTServiceClient' => function ( MediaWikiServices
$services ) {
407 $config = $services->getMainConfig()->get( 'VirtualRestConfig' );
409 $vrsClient = new VirtualRESTServiceClient( new MultiHttpClient( [] ) );
410 foreach ( $config['paths'] as $prefix => $serviceConfig ) {
411 $class = $serviceConfig['class'];
412 // Merge in the global defaults
413 $constructArg = isset( $serviceConfig['options'] )
414 ?
$serviceConfig['options']
416 $constructArg +
= $config['global'];
417 // Make the VRS service available at the mount point
418 $vrsClient->mount( $prefix, [ 'class' => $class, 'config' => $constructArg ] );
424 'ConfiguredReadOnlyMode' => function ( MediaWikiServices
$services ) {
425 return new ConfiguredReadOnlyMode( $services->getMainConfig() );
428 'ReadOnlyMode' => function ( MediaWikiServices
$services ) {
429 return new ReadOnlyMode(
430 $services->getConfiguredReadOnlyMode(),
431 $services->getDBLoadBalancer()
435 'ShellCommandFactory' => function ( MediaWikiServices
$services ) {
436 $config = $services->getMainConfig();
439 'time' => $config->get( 'MaxShellTime' ),
440 'walltime' => $config->get( 'MaxShellWallClockTime' ),
441 'memory' => $config->get( 'MaxShellMemory' ),
442 'filesize' => $config->get( 'MaxShellFileSize' ),
444 $cgroup = $config->get( 'ShellCgroup' );
445 $restrictionMethod = $config->get( 'ShellRestrictionMethod' );
447 $factory = new CommandFactory( $limits, $cgroup, $restrictionMethod );
448 $factory->setLogger( LoggerFactory
::getInstance( 'exec' ) );
449 $factory->logStderr();
454 'ExternalStoreFactory' => function ( MediaWikiServices
$services ) {
455 $config = $services->getMainConfig();
457 return new ExternalStoreFactory(
458 $config->get( 'ExternalStores' )
462 'RevisionStore' => function ( MediaWikiServices
$services ) {
463 /** @var SqlBlobStore $blobStore */
464 $blobStore = $services->getService( '_SqlBlobStore' );
466 $store = new RevisionStore(
467 $services->getDBLoadBalancer(),
469 $services->getMainWANObjectCache()
472 $config = $services->getMainConfig();
473 $store->setContentHandlerUseDB( $config->get( 'ContentHandlerUseDB' ) );
478 'BlobStoreFactory' => function ( MediaWikiServices
$services ) {
480 return new BlobStoreFactory(
481 $services->getDBLoadBalancer(),
482 $services->getMainWANObjectCache(),
483 $services->getMainConfig(),
488 'BlobStore' => function ( MediaWikiServices
$services ) {
489 return $services->getService( '_SqlBlobStore' );
492 '_SqlBlobStore' => function ( MediaWikiServices
$services ) {
493 return $services->getBlobStoreFactory()->newSqlBlobStore();
496 ///////////////////////////////////////////////////////////////////////////
497 // NOTE: When adding a service here, don't forget to add a getter function
498 // in the MediaWikiServices class. The convenience getter should just call
499 // $this->getService( 'FooBarService' ).
500 ///////////////////////////////////////////////////////////////////////////